home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Boxer / PalmBoxer / untar.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.6 KB  |  155 lines

  1. /* based on funzip.c from unzip 5.40 */
  2.  
  3. #define IGNORE_STDIO_STUBS
  4. #define __string_h
  5.  
  6. #ifdef OLDGCC
  7.  
  8. #include <Common.h>
  9. #include <System/SysAll.h>
  10. #include <UI/UIAll.h>
  11. #include <Unix/sys_types.h>
  12.  
  13. #else
  14.  
  15. #include <PalmOS.h>
  16. #include <PalmCompatibility.h>
  17. #include <Unix/sys_types.h>
  18.  
  19. #endif
  20.  
  21. #include "stringil.h"
  22. #include "stdio2.h"
  23.  
  24. #ifdef __PALMOS_TRAPS__
  25. Err errno;
  26. #endif
  27.  
  28. extern int fgetc(FILE * fp);
  29. extern FILE *fopen(char *name, char *mode);
  30.  
  31. int untardir(FILE * fd, char *dir, long int *loc)
  32. {
  33.   char ibuf[512], *c;
  34.   unsigned long total;
  35.   int max;
  36.  
  37.   max = 0;
  38.   *dir = 0;
  39.   dir[1]= 0;
  40.  
  41.   fseek(fd, 0, SEEK_SET);
  42.  
  43.   fread(ibuf, 1, 512, fd);
  44.   if (strncmp(&ibuf[257], "GNUtar",6) && strncmp(&ibuf[257], "ustar", 5))
  45.     return 0;
  46.  
  47.   fseek(fd, 0, SEEK_SET);
  48.  
  49.   for (;;) {
  50.     fread(ibuf, 1, 512, fd);
  51.  
  52.     if (ibuf[0] == 0)
  53.       fread(ibuf, 1, 512, fd);
  54.  
  55.     if (strncmp(&ibuf[257], "GNUtar",6) && strncmp(&ibuf[257], "ustar", 5))
  56.       return max;
  57.  
  58.     *loc++ = ftell(fd) - 512L;
  59.  
  60.     strcpy(dir,ibuf);
  61.     dir += strlen(dir);
  62.     *++dir = 0;
  63.  
  64.     c = &ibuf[124];
  65.     while (*c == ' ')
  66.       c++;
  67.     total = 0;
  68.  
  69.     do {
  70.       total = (total << 3) + *c++ - '0';
  71.     } while (*c != ' ');
  72.  
  73.     // round to the block size
  74.     total += 512;
  75.     total &= ~511;
  76.     fseek(fd, total, SEEK_CUR);
  77.     max++;
  78.  
  79.   }
  80.  
  81.  
  82. }
  83.  
  84. int untar(FILE * fd, long int loc)
  85. {
  86.   char ibuf[512], *c;
  87.   unsigned long total, itot;
  88.   FILE *ofd;
  89.   RectangleType r;
  90.  
  91.   fseek(fd, 0, SEEK_SET);
  92.  
  93.   fread(ibuf, 1, 512, fd);
  94.   if (strncmp(&ibuf[257], "GNUtar",6) && strncmp(&ibuf[257], "ustar", 5))
  95.     return -1;
  96.  
  97.   if( loc == -1 ) {
  98.     fseek(fd, 0, SEEK_SET);
  99.     FileControl(fileOpDestructiveReadMode, fd, NULL, NULL);
  100.   }
  101.   else
  102.     fseek(fd, loc, SEEK_SET);
  103.  
  104.   for (;;) {
  105.  
  106.     fread(ibuf, 1, 512, fd);
  107.  
  108.     if (ibuf[0] == 0)
  109.       fread(ibuf, 1, 512, fd);
  110.  
  111.     if (strncmp(&ibuf[257], "GNUtar",6) && strncmp(&ibuf[257], "ustar", 5))
  112.       return 0;
  113.  
  114.     r.topLeft.x = 0, r.topLeft.y = 15, r.extent.x = 160, r.extent.y = 30;
  115.     WinEraseRectangle(&r, 0);
  116.  
  117.     r.topLeft.x = 0, r.topLeft.y = 30, r.extent.x = 160, r.extent.y = 15;
  118.  
  119.  
  120.     c = &ibuf[124];
  121.     while (*c == ' ')
  122.       c++;
  123.     total = 0;
  124.  
  125.     do {
  126.       total = (total << 3) + *c++ - '0';
  127.     } while (*c != ' ');
  128.  
  129.     ofd = fopen(ibuf, "w");
  130.  
  131.  
  132.     StrIToA(ibuf,total);
  133.     strcat(ibuf,"     ");
  134.     WinDrawChars(ibuf,strlen(ibuf),120,0);
  135.  
  136.  
  137.     itot = total;
  138.     while (total) {
  139.  
  140.       fread(ibuf, 1, 512, fd);
  141.       fwrite(ibuf, 1, total > 512 ? 512 : total, ofd);
  142.       total -= total > 512 ? 512 : total;
  143.       r.extent.x = 160 - (160 * total / itot);
  144.       WinDrawRectangle(&r, 0);
  145.     }
  146.     fclose(ofd);
  147.  
  148.     if( loc != -1 )
  149.       break;
  150.  
  151.   }
  152.   return 0;
  153.  
  154. }
  155.